﻿#include <QQuickView>
#include "hellowordhandlemessage.h"
// CTKPluginFramework includes
#include "ctkPluginContext.h"
#include "ctkPluginFrameworkLauncher.h"
#include "hellowordservice.h"
#include "../MainWindow/mainwindowservice.h"


HellowordHandleMessage::HellowordHandleMessage(QObject *parent)
    : QObject(parent)
{

}

void HellowordHandleMessage::postCtkEvent(const QString event, const QVariant data, const QVariant extentionData)
{
    ctkPluginContext *context = ctkPluginFrameworkLauncher::getPluginContext();
    ctkServiceReference ref = context->getServiceReference<HelloWordService>();
    if (ref) {
        HelloWordService* service = context->getService<HelloWordService>(ref);
        if (service != nullptr) {
            service->postCtkEvent(event, data, extentionData);
            qInfo() << "event: " << event << "data: " << data << "extentionData: " <<extentionData;
            context->ungetService(ref);
        }
    } else {
       qFatal("HellowordHandleMessage postCtkEvent failed.");
    }
}

QQuickView* HellowordHandleMessage::getMainView()
{
    QQuickView* view = nullptr;
    // mainWindow
    ctkPluginContext *context = ctkPluginFrameworkLauncher::getPluginContext();
    ctkServiceReference ref = context->getServiceReference<MainWindowService>();
    if (ref) {
       MainWindowService* mainWindow = context->getService<MainWindowService>(ref);
       if (mainWindow != nullptr) {
            view = mainWindow->getQuickView();
            context->ungetService(ref);
       }
    } else {
       qFatal("getMainView pointer is null.");
    }

    return view;
}
